package com.xunshibao.util;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public final class OAuthUtils {
	private static final String SinaAppID = "4111114133";
	private static final String SinaAppSecret = "cdc2186d78f20618bd734fc271759513";
	
	public static String genSinaSign(String method, String baseUri, Map<String, String> params) {
		Set<String> keySet = params.keySet();
		List<String> keys = new ArrayList<String>(keySet.size());
		keys.addAll(keySet);
		Collections.sort(keys);
		StringBuilder joinString = new StringBuilder();

		joinString.append(method);
		joinString.append("&");
		joinString.append(URLEncoder.encode(baseUri));
		joinString.append("&");
		
		int i = 0;
		for(String key : keys) {
			joinString.append(key);
			joinString.append("%3D");
			joinString.append(params.get(key));
			if(i > 0) {
				joinString.append("%26");
			}
			i++;
		}
		return joinString.toString();
	}
	
	public static void genSign() {
	}
	
	public static void main(String[] args) {
		Map<String, String> params = new HashMap<String, String>();
		params.put("consumer secret", "MCD8BKwGdgPHvAuvgvz4EQpqDAtx89grbuNMRd7Eh9");
		params.put("oauth_callback", "http://localhost:3005/the_dance/process_callback?service_provider_id=11");
		params.put("oauth_consumer_key", "GDdmIQH6jhtmLUypg82g");
		params.put("oauth_nonce", "QP70eNmVz8jvdPevU3oJD2AfF7R7odC2XJcn4XlZJqk");
		params.put("oauth_signature_method", "HMAC-SHA1");
		params.put("oauth_timestamp", "1272323042");
		params.put("oauth_version", "1.0");
		String baseString = genSinaSign("POST", "http://api.t.sina.com.cn/oauth/request_token", params);
		System.out.println(baseString);
		String key = new String(StringEncryptor.hmacSHA1(SinaAppSecret, baseString));
		System.out.println(key);
	}
}
